﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Crossroad.System.Api;
using Crossroad.System.Common.Service;
using Crossroad.Utils.Extensions;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace Crossroad.System.Common.Controller
{
    [Route("api/system/common/areas")]
    public class AreaController : BaseApi
    {
        private readonly IAreaCnService areaCnService;

        public AreaController(
            IAreaCnService areaCnServiceIpt
        )
        {
            areaCnService = areaCnServiceIpt;
        }

        /// <summary>
        /// 中国省市区数据
        /// </summary>
        /// <param name="parentId"></param>
        /// <returns></returns>
        [HttpGet("cn")]
        public object CnData(
            int? parentId = null
        )
        {
            return areaCnService.GetList(queryable =>
            {
                if (parentId.NotNull())
                    queryable.Where(a => a.parentId == parentId);
                else
                    queryable.Where(a => a.id > 0 && a.parentId == 0);
            });
        }


    }
}